﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： GovLogLoginRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using CGT.Infrastructure.Entity;
using Dapper.Extensions;
using CGT.Infrastructure.Dto;
using Dapper;
using System.Linq;

namespace CGT.Infrastructure.Repository
{
    /// <summary>
    /// 仓储层
    /// </summary>	
    public class GovLogLoginRepository : BaseRepository<GovLogLogin>
    {

        #region 构造

        public GovLogLoginRepository() : base() { }

        public GovLogLoginRepository(string connectionString) : base(connectionString) { }

        public GovLogLoginRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 获取固定时间端的登陆日志
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public List<GovLogLogin> GetList(int userId, DateTime beginDate, DateTime endDate)
        {
            string sql = "SELECT* FROM GovLogLogin WITH(NOLOCK)WHERE UserId = @UserID AND CreatedDate > @BeginDate AND CreatedDate < @EndDate";
            return Query(sql, new { UserID = userId, BeginDate = beginDate, EndDate = endDate });
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Page<GovLogLogin> GetLastTimeLoginFailPages(int userId, DateTime beginDate, DateTime endDate,long pageIndex, long pageSize)
        {
            string sql = "SELECT * FROM GovLogLogin WITH(NOLOCK) WHERE UserId = @UserID AND CreatedDate > @BeginDate AND CreatedDate <= @EndDate and IncidentStatus=1 and IncidentType=1";
            return dbContext.Connection.Page<GovLogLogin>(pageIndex, pageSize, sql, new { UserID = userId, BeginDate = beginDate, EndDate = endDate });
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="incidentType"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Page<GovLogLoginDto> GetList(ScLogLogin condition)
        {
            StringBuilder sb = new StringBuilder("SELECT o.*,l.realname as UserName FROM GovLogLogin o WITH(NOLOCK) LEFT JOIN GovUser l WITH(NOLOCK) ON l.userid=o.userid WHERE l.IsActive=1");
            if (condition.IncidentType>0)
            {
                sb.Append(" AND IncidentType = @IncidentType");
            }
            if (condition.LogId > 0)
            {
                sb.Append(" AND LogId = @LogId");
            }
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sb.Append(" AND CreatedDate > @BeginDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sb.Append(" AND CreatedDate <= DATEADD(dd,1,@EndDate)");
            }
            sb.Append(" Order By LogId DESC");
            return dbContext.Connection.Page<GovLogLoginDto>(condition.PageIndex, condition.PageSize, sb.ToString(), new { IncidentType = condition.IncidentType, LogId=condition.LogId, BeginDate = condition.StartDate, EndDate = condition.EndDate });
        }

        /// <summary>
        /// 查询统计信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<LoginStatisicDto> SearchStatisticalInformation(ScLogLogin condition)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select CONVERT(varchar(100), CreatedDate, 102) as StatisicDate,count(1) as StatisicCount from [GovLogLogin] with(nolock) where 1=1");

            if (condition.StartDate != "")
            {
                sb.Append(" and CreatedDate>=@StartDate");
            }
            if (condition.EndDate != "")
            {
                sb.Append(" and CreatedDate<DATEADD(dd,1,@EndDate)");
            }
            if (condition.IncidentType > 0)
            {
                sb.Append(" and IncidentType=@IncidentType");
            }
            sb.Append(" group by CONVERT(varchar(100), CreatedDate, 102)");
            return dbContext.Connection.Query<LoginStatisicDto>(sb.ToString(), new { StartDate = condition.StartDate, EndDate=condition.EndDate, IncidentType=condition.IncidentType }).ToList();
        }
    }
}